import web
import requests
import json
import subprocess

urls = (
    '/', 'index',
    '/dashboard', 'dashboard',
    '/images', 'images',
    '/containers', 'containers',
    '/devices', 'devices',
    '/imageList', 'imageList',
    '/deploy', 'deploy',
    '/deviceList', 'deviceList',
)

#修改为实际的harbor的IP与端口
harborApiUrl = 'http://localhost:8088/api/v2.0/'

#web
render = web.template.render('')

class MyApplication(web.application):
    def run(self, port=8080, *middleware):
        func = self.wsgifunc(*middleware)
        return web.httpserver.runsimple(func, ('0.0.0.0', port))

class index:
    def GET(self):
        return render.dashboard()
class dashboard:
    def GET(self):
        return render.dashboard()
class images:
    def GET(self):
        return render.images()
class containers:
    def GET(self):
        return render.containers()
class devices:
    def GET(self):
        return render.devices()

class deviceList:
    def GET(self):
        paras = web.input()
        showOnline = paras.get("online")
        file = open("devices.txt", "r", encoding='utf-8')
        data = json.load(file)
        for device in data:
            res = subprocess.run(['ping', '-c', '2', '-w', '1', device["ip"]],stdout=subprocess.PIPE)
            return_ret = str(res.stdout)
            if 'icmp_seq=' in return_ret and 'ttl=' in return_ret:
                device["status"] = "Online"
            else:
                if showOnline:
                    data.remove(device)
                else:
                    device["status"] = "Offline"
        return json.dumps(data)

class deploy:
    def POST(self):
        ip = str(web.data(), encoding="utf-8")
        #需要修改脚本
        command = "sudo ssh root@"+ip+" \"bash -x /data/aric/update_app_img.sh\""
        process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        process.wait()

class imageList:
    def GET(self):
        result=[]
        projectRes = requests.get(harborApiUrl+'projects')
        projectList = json.loads(projectRes.content)
        for project in projectList:
            projectName = project.get("name")
            repositoryRes = requests.get(harborApiUrl+'projects/'+projectName+'/repositories')
            repositoryList = json.loads(repositoryRes.content)
            for repository in repositoryList:
                repositoryName = repository.get("name").replace(projectName+"/","")
                artifactRes = requests.get(harborApiUrl+'projects/'+projectName+'/repositories/'+
                repositoryName+'/artifacts')
                artifactList = json.loads(artifactRes.content)
                for artifact in artifactList:
                    artifactDigest = artifact.get("digest")
                    tagRes = requests.get(harborApiUrl+'projects/'+projectName+'/repositories/'+
                    repositoryName+'/artifacts/'+artifactDigest+'/tags')
                    tagList = json.loads(tagRes.content)
                    for tag in tagList:
                        if tag.get("name")!='':
                            result.append({"project":projectName,"repository":repositoryName,"tag":tag.get("name"),"push_time":tag.get("push_time")})

        return result

if __name__ == "__main__":
    app = MyApplication(urls ,globals())
    app.run(port=8081)